package com.stox.shared.core.model.filter;

import java.util.Date;

import com.stox.shared.core.model.enumeration.BarSpan;
import com.stox.shared.core.model.exception.StoxException;

public class BarCriteria implements Cloneable{

	private Long scripId;
	private Date from;
	private Date to;
	private BarSpan barSpan;
	
	public BarCriteria() {
		
	}
	
	public BarCriteria(Long scripId, Date from, Date to, BarSpan barSpan) {
		super();
		this.scripId = scripId;
		this.from = from;
		this.to = to;
		this.barSpan = barSpan;
	}
	
	@Override
	public BarCriteria clone(){
		BarCriteria clone = new BarCriteria();
		clone.scripId = scripId;
		clone.from = new Date(from.getTime());
		clone.to = new Date(to.getTime());
		clone.barSpan = barSpan;
		return clone;
	}

	@SuppressWarnings("deprecation")
	public void validate(){
		if(null == scripId) throw new StoxException("Scrip Id can not be null");
		if(null == barSpan) barSpan = BarSpan.DAILY;
		if(null == from) from = new Date(0,0,0);
		if(null == to) to = new Date();
	}
	
	public Long getScripId() {
		return scripId;
	}
	public void setScripId(Long scripId) {
		this.scripId = scripId;
	}
	public Date getFrom() {
		return from;
	}
	public void setFrom(Date from) {
		this.from = from;
	}
	public Date getTo() {
		return to;
	}
	public void setTo(Date to) {
		this.to = to;
	}
	public BarSpan getBarSpan() {
		return barSpan;
	}
	public void setBarSpan(BarSpan barSpan) {
		this.barSpan = barSpan;
	}
	
	@Override
	public String toString() {
		return "BarCrieria [scripId=" + scripId + ", from=" + from + ", to="
				+ to + ", barSpan=" + barSpan + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((barSpan == null) ? 0 : barSpan.hashCode());
		result = prime * result + ((from == null) ? 0 : from.hashCode());
		result = prime * result + ((scripId == null) ? 0 : scripId.hashCode());
		result = prime * result + ((to == null) ? 0 : to.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		BarCriteria other = (BarCriteria) obj;
		if (barSpan != other.barSpan)
			return false;
		if (from == null) {
			if (other.from != null)
				return false;
		} else if (!from.equals(other.from))
			return false;
		if (scripId == null) {
			if (other.scripId != null)
				return false;
		} else if (!scripId.equals(other.scripId))
			return false;
		if (to == null) {
			if (other.to != null)
				return false;
		} else if (!to.equals(other.to))
			return false;
		return true;
	}
	
}
